<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container">
    <div class="tab-box">
      <nav class="tab">
        <a
          href="javascript:;"
          @click="comName = 'CallbackBind'"
          :class="{ active: comName === 'CallbackBind' }"
        >
          <i class="iconfont icon-bind" />
          <span>已有小兔鲜账号，请绑定手机</span>
        </a>
        <a
          href="javascript:;"
          @click="comName = 'CallbackPatch'"
          :class="{ active: comName === 'CallbackPatch' }"
        >
          <i class="iconfont icon-edit" />
          <span>没有小兔鲜账号，请完善资料</span>
        </a>
      </nav>

      <div class="tab-content">
        <!-- <CallbackBind></CallbackBind>
        <CallbackPatch></CallbackPatch> -->
        <component :is="comName" :openId="qqOpenId"></component>
      </div>
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import CallbackBind from './components/callback-bind.vue'
import CallbackPatch from './components/callback-patch.vue'
import { ref } from 'vue'

// QQ 互联
import QC from 'qc'
import { reqUserQQLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { message } from '@/components/Message'
export default {
  name: 'PageCallback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup () {
    const qqOpenId = ref('')
    const comName = ref('CallbackBind')
    // 查看登录状态是否有效
    const store = useStore()
    const router = useRouter()
    if (QC.Login.check()) {
      // 检查QQ是否登录
      QC.Login.getMe((openId) => {
        // 1. 收集openId，将来供子组件使用的
        qqOpenId.value = openId
        reqUserQQLogin(openId)
          .then((res) => {
            console.log(res, 88)
            // 使用qq登录成功
            // 1. 存储用户信息
            store.commit('user/setProfile', res.result)
            // 2. 跳转首页
            router.push('/')
            // 3. 成功提示
            message({ type: 'success', text: 'QQ登录成功' })
          })
          .catch((e) => {
            // 使用qq登录失败
            console.log('qq还未绑定过账号')
          })
      })
    }
    return { comName, qqOpenId }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
